// vim: sw=4:ts=4:nu:nospell:fdc=4
/* global Ext, Example */
/**
 * Form Load And Submit Example
 * 
 * @author Ing. Jozef Sakáloš
 * @copyright (c) 2008, by Ing. Jozef Sakáloš
 * @date 12. April 2008
 * @version $Id: formloadsubmit.js 149 2009-06-03 08:02:07Z jozo $
 * 
 * @license formloadsubmit.js is licensed under the terms of the Open Source
 *          LGPL 3.0 license. Commercial use is permitted to the extent that the
 *          code/component(s) do NOT become part of another Open Source or
 *          Commercially licensed development library or toolkit without
 *          explicit permission.
 * 
 * License details: http://www.gnu.org/licenses/lgpl.html
 */

Ext.ns('Example');

Example.Form = Ext.extend(Ext.form.FormPanel, {

			// defaults - can be changed from outside
			border : false,
			frame : true,
			labelWidth : 80,
			url : 'formloadsubmit.php'

			,
			constructor : function(config) {
				config = config || {};
				config.listeners = config.listeners || {};
				Ext.applyIf(config.listeners, {
							actioncomplete : function() {
								if (console && console.log) {
									console.log('actioncomplete:', arguments);
								}
							},
							actionfailed : function() {
								if (console && console.log) {
									console.log('actionfailed:', arguments);
								}
							}
						});
				Example.Form.superclass.constructor.call(this, config);
			}

			,
			initComponent : function() {

				// hard coded - cannot be changed from outsid
				var config = {
					defaultType : 'textfield',
					defaults : {
						anchor : '-24'
					},
					monitorValid : true,
					autoScroll : true
					// ,buttonAlign:'right'
					,
					items : [{
								name : 'firstName',
								fieldLabel : 'First Name'
							}, {
								name : 'lastName',
								fieldLabel : 'Last Name',
								allowBlank : false
							}, {
								name : 'email',
								fieldLabel : 'E-mail',
								vtype : 'email'
							}, {
								name : 'friend',
								fieldLabel : 'Friend',
								xtype : 'xcheckbox'
							}, {
								name : 'owed',
								fieldLabel : 'Owed Sum',
								xtype : 'numberfield'
							}, {
								name : 'category',
								fieldLabel : 'Category',
								xtype : 'combo',
								triggerAction : 'all',
								mode : 'local',
								editable : false,
								store : new Ext.data.SimpleStore({
											fields : ['catCode', 'catName'],
											data : [['cat1', 'Category 1'],
													['cat2', 'Category 2']]
										}),
								displayField : 'catName',
								valueField : 'catCode',
								hiddenName : 'category'
							}, {
								name : 'note',
								fieldLabel : 'Note',
								xtype : 'textarea'
							}],
					buttons : [{
								text : 'Load',
								scope : this,
								handler : this.onLoadClick
							}, {
								text : 'Submit',
								formBind : true,
								scope : this,
								handler : this.submit
							}]
				}; // eo config object

				// apply config
				Ext.apply(this, Ext.apply(this.initialConfig, config));

				// call parent
				Example.Form.superclass.initComponent.apply(this, arguments);

			} // eo function initComponent

			/**
			 * Form onRender override
			 */
			,
			onRender : function() {

				// call parent
				Example.Form.superclass.onRender.apply(this, arguments);

				// set wait message target
				this.getForm().waitMsgTarget = this.getEl();

				// loads form after initial layout
				// this.on('afterlayout', this.onLoadClick, this,
				// {single:true});

			} // eo function onRender

			/**
			 * Load button click handler
			 */
			,
			onLoadClick : function() {
				this.load({
							url : this.url,
							waitMsg : 'Loading...',
							params : {
								cmd : 'load'
							}
						});
				// any additional load click processing here
			} // eo function onLoadClick

			/**
			 * Submits the form. Called after Submit buttons is clicked 144. *
			 * 
			 * @private 145.
			 */
			,
			submit : function() {
				this.getForm().submit({
							url : this.url,
							scope : this,
							success : this.onSuccess,
							failure : this.onFailure,
							params : {
								cmd : 'save'
							},
							waitMsg : 'Saving...'
						});
			} // eo function submit

			/**
			 * 58. * Success handler 159. *
			 * 
			 * @param {Ext.form.BasicForm}
			 *            form 160. *
			 * @param {Ext.form.Action}
			 *            action 161. *
			 * @private 162.
			 */
			,
			onSuccess : function(form, action) {
				Ext.Msg.show({
							title : 'Success',
							msg : 'Form submitted successfully',
							modal : true,
							icon : Ext.Msg.INFO,
							buttons : Ext.Msg.OK
						});
			} // eo function onSuccess

			/**
			 * 174. * Failure handler 175. *
			 * 
			 * @param {Ext.form.BasicForm}
			 *            form 176. *
			 * @param {Ext.form.Action}
			 *            action 177. *
			 * @private 178.
			 */
			,
			onFailure : function(form, action) {
				this.showError(action.result.error
						|| action.response.responseText);
			} // eo function onFailure

			/**
			 * Shows Message Box with error 185. *
			 * 
			 * @param {String}
			 *            msg Message to show 186. *
			 * @param {String}
			 *            title Optional. Title for message box (defaults to
			 *            Error) 187. *
			 * @private 188.
			 */
			,
			showError : function(msg, title) {
				title = title || 'Error';
				Ext.Msg.show({
							title : title,
							msg : msg,
							modal : true,
							icon : Ext.Msg.ERROR,
							buttons : Ext.Msg.OK
						});
			} // eo function showError

		}); // eo extend

// register xtype
Ext.reg('exampleform', Example.Form);

Ext.BLANK_IMAGE_URL = './ext/resources/images/default/s.gif';

// application main entry point
Ext.onReady(function() {

			Ext.QuickTips.init();

			// invalid markers to sides
			Ext.form.Field.prototype.msgTarget = 'side';

			// create and show window
			var win = new Ext.Window({
						id : 'formloadsubmit-win',
						title : Ext.fly('page-title').dom.innerHTML,
						layout : 'fit',
						width : 350,
						height : 320,
						closable : false,
						border : false,
						items : {
							id : 'formloadsubmit-form',
							xtype : 'exampleform'
						}
					});

			win.show();

		}); // eo function onReady

// eof
